Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
Naming Convention for Methods and Properties in Swift. Can I use "get", "set" and "is" Keywords?

When I create a function in Swift, can I use "get" and "set" words? For instance can I declare a function as func getAuthorizedUsers() instead of func authorizedUsers()? In ObjectiveC get and set keywords are not suggested to use when declaring functions. How about in Swift?

Also, when declaring properties can I use "is" keyword? For instance:

public var isAuthorized: Bool {
    get {            
        return true
    }
}

I have read Swift naming convention documents but I couldn't find the answer of my question. Thank you.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The rules are all outlined here.

For get, that clearly violates the "Omit needless words" rule. If a method returns something, the call site will know that it is used to get some value. You don't need to repeat that idea. You can consider turning this into a computed property if no parameters are required.

For set, it might be appropriate sometimes. If your method only need one parameter and there is a corresponding getter,

func getFoo() -> Int {
    ...
}

func setFoo(_ foo: Int) {
    ...
}

That's a pretty good sign that this can be turned into a computed property:

var foo: Int {
    get { ... }
    set { ... }
}

A good example where it is appropriate to have set is the UIButton.setTitle method. It takes in two parameters, so a computed property wouldn't work.

For is, that clearly conforms to the rule "Uses of Boolean methods and properties should read as assertions about the receiver". So yes, you should use it for boolean members.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda